package com.yanqu.road.dao.impl.activity.deepfish;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.deepfishing.DeepFishingUserData;
import com.yanqu.road.entity.activity.deepfishing.entity.DeepFishingBeachCombItemData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class DeepFishingUserDataDaoImpl extends TempDataObjectDao<DeepFishingUserData> {
    public static String getDeepFishingUserDataSql = "select * from `t_u_deep_fishing_user` where `activityId`=? and `userId`=?";
    public DeepFishingUserData getDeepFishingUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDeepFishingUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateDeepFishingUserDataSql = "update `t_u_deep_fishing_user` set beachCombLevel=?,beachCombLastRefreshTime=?,beachCombSpecialThisLevel=?,beachCombHasSpecialThisLevel=?,beachCombMap=?,beachCombPower=?,lastRecoveryBeachCombPowerTime=?,fishingReachMaxMeter=?,fishingBuffMap=?,fishingRodParts=?,fishingCoins=?,fishingPower=?,lastRecoveryFishingPowerTime=?,fishingGameStatus=?,noBuffBiggestCoin=?,bestAutoRecord=?,scoreThisActivity=?,triggerGuide=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(DeepFishingUserData deepFishingUserData) {
        deepFishingUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getBeachCombLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserData.getBeachCombLastRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getBeachCombSpecialThisLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getBeachCombHasSpecialThisLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(deepFishingUserData.getBeachCombMap())));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getBeachCombPower()));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserData.getLastRecoveryBeachCombPowerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getFishingReachMaxMeter()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(deepFishingUserData.getFishingBuffMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(deepFishingUserData.getFishingRodParts())));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserData.getFishingCoins()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getFishingPower()));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserData.getLastRecoveryFishingPowerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getFishingGameStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getNoBuffBiggestCoin()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(deepFishingUserData.getBestAutoRecord())));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserData.getScoreThisActivity()));
        para.put(i++, new DbParameter(Types.BOOLEAN, deepFishingUserData.isTriggerGuide()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserData.getUserId()));
        if (getDb().executeNoneQuery(updateDeepFishingUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDeepFishingUserDataSql = "insert into `t_u_deep_fishing_user`(activityId,userId,beachCombLevel,beachCombLastRefreshTime,beachCombSpecialThisLevel,beachCombHasSpecialThisLevel,beachCombMap,beachCombPower,lastRecoveryBeachCombPowerTime,fishingReachMaxMeter,fishingBuffMap,fishingRodParts,fishingCoins,fishingPower,lastRecoveryFishingPowerTime,fishingGameStatus,noBuffBiggestCoin,bestAutoRecord,scoreThisActivity,triggerGuide) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(DeepFishingUserData deepFishingUserData) {
        deepFishingUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getBeachCombLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserData.getBeachCombLastRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getBeachCombSpecialThisLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getBeachCombHasSpecialThisLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(deepFishingUserData.getBeachCombMap())));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getBeachCombPower()));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserData.getLastRecoveryBeachCombPowerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getFishingReachMaxMeter()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(deepFishingUserData.getFishingBuffMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(deepFishingUserData.getFishingRodParts())));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserData.getFishingCoins()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getFishingPower()));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserData.getLastRecoveryFishingPowerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getFishingGameStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserData.getNoBuffBiggestCoin()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(deepFishingUserData.getBestAutoRecord())));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserData.getScoreThisActivity()));
        para.put(i++, new DbParameter(Types.BOOLEAN, deepFishingUserData.isTriggerGuide()));
        if (getDb().executeNoneQuery(insertDeepFishingUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DeepFishingUserData parseData(ResultSet rs) throws SQLException {
        DeepFishingUserData deepFishingUserData = new DeepFishingUserData();
        deepFishingUserData.setActivityId(rs.getInt("activityId"));
        deepFishingUserData.setUserId(rs.getLong("userId"));
        deepFishingUserData.setBeachCombLevel(rs.getInt("beachCombLevel"));
        deepFishingUserData.setBeachCombLastRefreshTime(rs.getLong("beachCombLastRefreshTime"));
        deepFishingUserData.setBeachCombSpecialThisLevel(rs.getInt("beachCombSpecialThisLevel"));
        deepFishingUserData.setBeachCombHasSpecialThisLevel(rs.getInt("beachCombHasSpecialThisLevel"));
        deepFishingUserData.setBeachCombMap(JSON.parseObject(rs.getString("beachCombMap"), new TypeReference<Map<Integer, DeepFishingBeachCombItemData>>() {}));
        deepFishingUserData.setBeachCombPower(rs.getInt("beachCombPower"));
        deepFishingUserData.setLastRecoveryBeachCombPowerTime(rs.getLong("lastRecoveryBeachCombPowerTime"));
        deepFishingUserData.setFishingReachMaxMeter(rs.getInt("fishingReachMaxMeter"));
        deepFishingUserData.setFishingBuffMap(JSON.parseObject(rs.getString("fishingBuffMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        deepFishingUserData.setFishingRodParts(JSON.parseObject(rs.getString("fishingRodParts"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        deepFishingUserData.setFishingCoins(rs.getLong("fishingCoins"));
        deepFishingUserData.setFishingPower(rs.getInt("fishingPower"));
        deepFishingUserData.setLastRecoveryFishingPowerTime(rs.getLong("lastRecoveryFishingPowerTime"));
        deepFishingUserData.setFishingGameStatus(rs.getInt("fishingGameStatus"));
        deepFishingUserData.setNoBuffBiggestCoin(rs.getInt("noBuffBiggestCoin"));
        deepFishingUserData.setBestAutoRecord(JSON.parseObject(rs.getString("bestAutoRecord"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.deepfishing.entity.DeepFishingBagItemData>>() {}));
        deepFishingUserData.setScoreThisActivity(rs.getLong("scoreThisActivity"));
        deepFishingUserData.setTriggerGuide(rs.getBoolean("triggerGuide"));
        return deepFishingUserData;
    }

}
